package io.lkt.modules.sys.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BOrderInfoEntity;
import io.lkt.modules.sys.entity.BOrderVitalEntity;
import io.lkt.modules.sys.entity.SysUserEntity;
import io.lkt.modules.sys.entity.SysUserGzhEntity;
import io.lkt.modules.sys.service.BOrderInfoService;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sys/bordervital")
public class BOrderVitalController extends AbstractController{
    @Autowired
    private BOrderInfoService bOrderInfoService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:borderinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        System.out.println(params);
        SysUserEntity curUser= getUser();
//        params.put("curUserId",curUser.getUserId());
        List<SysUserGzhEntity> userGzhList = this.sysUserRoleService.queryGzhList(curUser.getUserId());
        params.put("userGzhList", userGzhList);

        if(params.get("startDate")!=null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        }
        if(params.get("endDate")!=null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate")+" 23:59:59");
        }
        Query query = new Query(params);

        List<BOrderVitalEntity> list=bOrderInfoService.queryVital(query);
        Integer total=bOrderInfoService.queryVitelTotal(query);
        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", page);
    }

    @RequestMapping("/queryMainPage")
    @RequiresPermissions("sys:borderinfo:list")
    public R queryMainPage(@RequestBody Map<String, Object> params) {

        SysUserEntity curUser= getUser();
        String startDate = DateUtil.getFormatDate("yyyy-MM-dd", new Date());
        String endDate = DateUtil.getFormatDate("yyyy-MM-dd", new Date()) + " 23:59:59";

        if (params.get("pDate") != null) {
            startDate = params.get("pDate").toString();
            endDate = params.get("pDate").toString() + " 23:59:59";
        }
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        //Query query = new Query(params);

        List<BOrderVitalEntity> list = bOrderInfoService.queryVital(params);
        BOrderVitalEntity ove = null;
        if (list != null && list.size() > 0) {
            ove = list.get(0);
        } else {
            ove = new BOrderVitalEntity();
            ove.setT1Count(0);
            ove.setTotalAmount(0d);
            ove.setT0Count(0);
            ove.setTotalAmountT1(0d);
            ove.setT2Count(0);
            ove.setTotalAmountT2(0d);
        }

        return R.ok().put("vital", ove)
                .put("curUserId",curUser.getUserId());
    }

    /**
     *
     */
    @RequestMapping("/vitalSum")
    @RequiresPermissions("sys:borderinfo:list")
    public R vital(@RequestBody Map<String, Object> params){
        System.out.println(params);
        SysUserEntity curUser= getUser();
//        params.put("curUserId",curUser.getUserId());

        List<SysUserGzhEntity> userGzhList = this.sysUserRoleService.queryGzhList(curUser.getUserId());
        params.put("userGzhList", userGzhList);

        if(params.get("startDate")!=null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        }
        if(params.get("endDate")!=null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate")+" 23:59:59");
        }

        BOrderVitalEntity bOrderVital=bOrderInfoService.queryVitalSum(params);
        BOrderVitalEntity bOrderRefun=bOrderInfoService.queryVitalRefundSum(params);
        if(bOrderVital!=null && bOrderRefun!=null){
            bOrderVital.setT2Count(bOrderRefun.getT2Count());
            bOrderVital.setTotalAmountT2(bOrderRefun.getTotalAmountT2());
        }

        return R.ok().put("vitalSum", bOrderVital);
    }
}
